﻿using System;
using System.Text;
using System.IO;


namespace MyFloridaHouse.libSvCommonUtilities
{
    public static class CompressionManager
    {
        
        public static byte[] Compress(string strInput)
        {
            try
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                int byteCount = utf8.GetByteCount(strInput);
                string inStrByteCount = byteCount.ToString();
                byte[] bytData = System.Text.Encoding.UTF8.GetBytes(strInput);
                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms, defl);
                s.Write(bytData, 0, bytData.Length);
                s.Close();
                byte[] compressedData = (byte[])ms.ToArray();
                byteCount = compressedData.Length;
                string outByteCount = byteCount.ToString();
                return compressedData;
            }
            catch
            {
                throw;
            }
        }

        public static byte[] Compress(byte[] bytData)
        {
            try
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                //int byteCount = byteInput.Length;
                //string inStrByteCount = byteCount.ToString();
                //byte[] bytData = System.Text.Encoding.UTF8.GetBytes(byteInput);
                MemoryStream ms = new MemoryStream();
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9, false);
                Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms, defl);
                s.Write(bytData, 0, bytData.Length);
                s.Close();
                byte[] compressedData = (byte[])ms.ToArray();
                //byteCount = compressedData.Length;
                //string outByteCount = byteCount.ToString();
                return compressedData;
            }
            catch (Exception ex)
            {
                string theMsg=ex.ToString();
                throw;
            }
        }

        public static string Decompress(byte[] bytInput)
        {              
            MemoryStream ms = new MemoryStream(bytInput,0,bytInput.Length);
                byte[] bytResult =null;
                string strResult=String.Empty;
                byte[] writeData = new byte[4096];
                  Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
            try
            {
                bytResult=ReadFullStream(s2);
                s2.Close();
                strResult = System.Text.Encoding.UTF8.GetString(bytResult, 0, bytResult.Length);
                return strResult;
            }
            catch (Exception ex)
            {
                string theMsg = ex.ToString();
                throw;
            }
        }

        public static byte[] DecompressToByteArray(byte[] bytInput)
        {
            MemoryStream ms = new MemoryStream(bytInput, 0, bytInput.Length);
            byte[] bytResult = null;
            //string strResult = String.Empty;
            byte[] writeData = new byte[4096];
            Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
            try
            {
                bytResult = ReadFullStream(s2);
                s2.Close();
                //strResult = System.Text.Encoding.UTF8.GetString(bytResult, 0, bytResult.Length);
                //return strResult;
                return bytResult;
            }
            catch (Exception ex)
            {
                string theMsg = ex.ToString();
                throw;
            }
        }

        public static byte[] ReadFullStream(Stream stream)
        {
            byte[] buffer = new byte[32768];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
    }
}
